package com.java.androidmarket.controller;

import com.java.androidmarket.dto.StockDTO;
import com.java.androidmarket.entity.Cart;
import com.java.androidmarket.entity.Result;
import com.java.androidmarket.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:张腾
 * date:2021-05-21
 */

@RestController
@CrossOrigin
@RequestMapping("stock")
@Api(tags = "库存模块")
public class StockController {

    @Autowired
    private StockService stockService;


    @PostMapping("check")
    @ApiOperation(value = "检查库存", notes = "检查库存是否满足需求")
    @ApiImplicitParam(dataType = "List<Cart>", name = "cartList", value = "购物车对象数组,存放商品id和欲购买数量")
    public Result checkStock(@RequestBody StockDTO stockDTO){

        List<Cart> cartList = stockDTO.getCartList();
        ArrayList<Integer> list = new ArrayList<>();
        for (Cart cart : cartList) {
            int goodsId = cart.getGoodsId();
            if (!stockService.checkStock(goodsId, cart.getGoodsNum())) {
                list.add(goodsId);
            }
        }
        if (list.size()!=0){
            return Result.error("-1","库存不足",list);
        }else{
            return Result.success();
        }
    }
}
